package com.miaosha.ms_05.service;

import com.miaosha.ms_05.dao.UserDao;
import com.miaosha.ms_05.domain.User;
import com.miaosha.ms_05.prefix.MiaoshaUserKey;
import com.miaosha.ms_05.prefix.UserKey;
import com.miaosha.ms_05.redis.JedisService;
import com.miaosha.ms_05.util.DBUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

/**
 * @Description //TODO
 * @Author Administrator
 * @DATE 2020/9/13 17:00
 **/
@Service
public class UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private JedisService jedisService;

    public User getUserById(String id) {
        return userDao.getUserById(id);
    }


    @Transactional
    public int batchInsert(List<User> userList) {
        if (CollectionUtils.isEmpty(userList)) {
            return 0;
        }
        int count = 0;
        for (User user : userList) {
            if (userDao.insert(user) > 0) {
                count++;
            }
        }
        return count;
    }

    public  void deleteUser() throws Exception {
        //删除文件
        File file = new File("E:\\idea_workspace_miaosha\\jmeter_data\\ms_05\\UserAndToken.txt");
        if(file.exists()) {
            file.delete();
        }
        //删除数据库
        userDao.delete();
        //删除缓存
        jedisService.deleteByKeyPrefix(MiaoshaUserKey.USERKEY_TOKEN_NAME);
    }
}
